<template>
  <div class="distribute-container">
    <!-- 搜索表单 -->
    <div class="search-form">
      <el-form :model="searchForm" inline>
        <el-form-item label="仓库名称">
          <el-input
            v-model="searchForm.depotName"
            placeholder="请输入仓库名称"
            clearable
          />
        </el-form-item>
    <el-form-item label="库位名称">
          <el-input
            v-model="searchForm.locationName"
            placeholder="请输入库位名称"
            clearable
          />
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="handleSearch">查询</el-button>
          <el-button @click="handleReset">重置</el-button>
        </el-form-item>
      </el-form>
    </div>

    <!-- 统计卡片 -->
    <div class="statistics-cards">
      <el-row :gutter="20">
        <el-col :span="6">
          <el-card class="stat-card current-stock">
            <div class="stat-number">{{ statistics.stocknum }}</div>
            <div class="stat-label">当前库存</div>
          </el-card>
        </el-col>
        <el-col :span="6">
          <el-card class="stat-card available-stock">
            <div class="stat-number">{{ statistics.usenum }}</div>
            <div class="stat-label">可用库存</div>
          </el-card>
        </el-col>
        <el-col :span="6">
          <el-card class="stat-card pending-outbound">
            <div class="stat-number">{{ statistics.waitnum }}</div>
            <div class="stat-label">待出库</div>
          </el-card>
        </el-col>
        <el-col :span="6">
          <el-card class="stat-card pending-inbound">
            <div class="stat-number">{{ statistics.exportnum }}</div>
            <div class="stat-label">待入库</div>
          </el-card>
        </el-col>
      </el-row>
    </div>

    <!-- 操作按钮 -->
    <div class="action-buttons">
    
      <el-button @click="handleRefresh">刷新</el-button>
    </div>

    <!-- 数据表格 -->
    <div class="table-container">
      <el-table 
        :data="tableData" 
        style="width: 100%;" 
        v-loading="loading"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" />
        <el-table-column type="index" label="序号" width="80" />
        <el-table-column prop="depotName" label="仓库名称" width="120" />
        <el-table-column prop="locationName" label="库位名称" width="120" />
        <el-table-column prop="stocknum" label="当前库存" width="100">
          <template #default="scope">
            <span class="stock-number">{{ scope.row.stocknum }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="usenum" label="可用库存" width="100">
          <template #default="scope">
            <span class="stock-number available">{{ scope.row.usenum }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="waitnum" label="待入库" width="100">
          <template #default="scope">
            <span class="stock-number pending-in">{{ scope.row.waitnum }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="exportnum" label="待出库" width="100">
          <template #default="scope">
            <span class="stock-number pending-out">{{ scope.row.exportnum }}</span>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!-- 分页 -->
    <div class="pagination-container">
      <el-pagination
        v-model:current-page="pagination.PageIndex"
        v-model:page-size="pagination.PageSize"
        :total="pagination.totalCount"
        :page-sizes="[15, 20, 50, 100]"
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
  </div>
</template>

<script lang="ts" setup>
import { ref, reactive, onMounted, computed } from 'vue'
import PurSale from '@/http/PurSale'
import { ElMessage } from 'element-plus'
import http from '@/http/axiosHelper'
// 搜索表单数据
const searchForm = reactive({
  productName: '',
  categoryName: '',
  specification: '',
  unitName: '',
  group: '',
  depotId: '',
  depotName: '',
  locationId: '',
  locationName: ''
})

// 分页数据
const pagination = reactive({
  PageIndex: 1,
  PageSize: 15,
  totalCount: 0,
  totalPage: 0
})

// 表格数据
const tableData = ref<any[]>([])
const loading = ref(false)
const selectedRows = ref<any[]>([])

// 统计数据
const statistics = reactive({
  stocknum: 0,
  usenum: 0,
  waitnum: 0,
  exportnum: 0
})

// 计算统计数据
const calculateStatistics = () => {
  const stats = {
   stocknum: 0,
  usenum: 0,
  waitnum: 0,
  exportnum: 0
  }
  
  tableData.value.forEach(item => {
    stats.stocknum += item.stocknum || 0
    stats.usenum += item.usenum || 0
    stats.waitnum += item.waitnum || 0
    stats.exportnum += item.exportnum || 0
  })
  
  Object.assign(statistics, stats)
}

// 搜索方法
const handleSearch = async () => {
  loading.value = true
  try {
    const params = {
      PageIndex: pagination.PageIndex,
      PageSize: pagination.PageSize,
      DepotId: searchForm.depotId,
      DepotName: searchForm.depotName,
      LocationId: searchForm.locationId,
      LocationName: searchForm.locationName,
      ProductName: searchForm.productName,
      CategoryName: searchForm.categoryName,
      Specification: searchForm.specification,
      UnitName: searchForm.unitName,
      Group: searchForm.group
    }
    
    try {
      const response = await http.get('/scmreadconsul/api/Ledger/ShowDistribute', { params })
      
      if (response.data.code === 200) {
        tableData.value = response.data.items || []
        pagination.totalCount = response.data.totalCount || 0
        pagination.totalPage = response.data.totalPage || 0
        calculateStatistics()
        ElMessage.success('查询成功')
      } else {
        ElMessage.error(response.data.msg || '查询失败')
      }
    } catch (apiError) {
      console.error('API调用失败:', apiError)
      // 如果API调用失败，使用模拟数据
      pagination.totalCount = 100
      pagination.totalPage = 10
      calculateStatistics()
      ElMessage.warning('使用模拟数据，API接口暂时不可用')
    }
  } catch (error) {
    console.error('查询失败:', error)
    ElMessage.error('查询失败，请稍后重试')
  } finally {
    loading.value = false
  }
}



// 重置搜索
const handleReset = () => {
  Object.keys(searchForm).forEach(key => {
    (searchForm as any)[key] = ''
  })
  pagination.PageIndex = 1
  handleSearch()
}



// 刷新数据
const handleRefresh = () => {
  handleSearch()
}

// 分页大小改变
const handleSizeChange = (size: number) => {
  pagination.PageSize = size
  pagination.PageIndex = 1
  handleSearch()
}

// 当前页改变
const handleCurrentChange = (page: number) => {
  pagination.PageIndex = page
  handleSearch()
}

// 选择行变化
const handleSelectionChange = (selection: any[]) => {
  selectedRows.value = selection
}

// 组件挂载时加载数据
onMounted(() => {
  handleSearch()
})
</script>

<style scoped>
.distribute-container {
  padding: 20px;
  background-color: #f5f5f5;
  min-height: 100vh;
}

.search-form {
  background-color: white;
  padding: 20px;
  border-radius: 8px;
  margin-bottom: 20px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.statistics-cards {
  margin-bottom: 20px;
}

.stat-card {
  text-align: center;
  border-radius: 8px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.stat-number {
  font-size: 28px;
  font-weight: bold;
  color: #409eff;
  margin-bottom: 8px;
}

.stat-label {
  font-size: 14px;
  color: #666;
}

.current-stock .stat-number {
  color: #409eff;
}

.available-stock .stat-number {
  color: #67c23a;
}

.pending-outbound .stat-number {
  color: #e6a23c;
}

.pending-inbound .stat-number {
  color: #f56c6c;
}

.action-buttons {
  background-color: white;
  padding: 15px 20px;
  border-radius: 8px;
  margin-bottom: 20px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.table-container {
  background-color: white;
  border-radius: 8px;
  padding: 20px;
  margin-bottom: 20px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.pagination-container {
  background-color: white;
  padding: 20px;
  border-radius: 8px;
  display: flex;
  justify-content: flex-end;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.el-form-item {
  margin-bottom: 15px;
}

.el-button {
  margin-right: 10px;
}

.el-table {
  margin-top: 10px;
}

.stock-number {
  font-weight: bold;
  font-size: 14px;
}

.stock-number.available {
  color: #67c23a;
}

.stock-number.pending-in {
  color: #f56c6c;
}

.stock-number.pending-out {
  color: #e6a23c;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .distribute-container {
    padding: 10px;
  }
  
  .statistics-cards .el-col {
    margin-bottom: 10px;
  }
  
  .stat-number {
    font-size: 24px;
  }
}
</style>
